package cn.cmcc.online.smsapi.ui.presenter;

import android.content.Context;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.cmcc.online.smsapi.ui.model.ActionData;
import cn.cmcc.online.smsapi.ui.model.HeadData;
import cn.cmcc.online.smsapi.ui.util.DataExchangeProxy;
import cn.cmcc.online.smsapi.ui.util.DataUtil;

public class GeneralBasePresenterImpl extends GeneralBasePresenter {

    @Override
    public void bind(JSONObject data) {
        super.bind(data);
        vista.initDefaultView();

        showHead();
        showBody();
        showBottom();
    }

    private void showHead() {
        HeadData data = new HeadData(cardData);
        vista.showHead(data);
    }

    private void showBottom() {
        List<ActionData> listAction = DataUtil.getActionList(cardData);
        if (listAction == null || listAction.size() == 0) {
            return;
        }

        vista.showBottom(listAction);
    }

    private void showBody() {
        if (DataUtil.isTypeCard(cardData)) {
            vista.showCard();
        } else {
            vista.showContent(DataUtil.getSmsContent(cardData));
        }
    }

    @Override
    public void switchCard() {
        DataUtil.switchCardType(cardData);
        showBody();
    }

    @Override
    public void processAction(Context context, ActionData data) {
        Map<String, String> extra = data.getExtra();
        if (extra == null) {
            extra = new HashMap<>();
        }

        try {
            JSONObject obj = DataUtil.getWebData(cardData);
            extra.put("bubbleJson", obj.toString());
            data.setExtra(extra);
        } catch (Exception e) {
            e.printStackTrace();
        }

        DataExchangeProxy.getInstance().processAction(context, data);
    }

    @Override
    public void updateBottom() {
        showBottom();
    }
}
